//
//  RegisterCodeCell.m
//  CedarFinancePH
//
//  Created by li guan chang on 2019/6/26.
//  Copyright © 2019 cedarhd. All rights reserved.
//

#import "RegisterCodeCell.h"

@interface RegisterCodeCell ()

@property (nonatomic,strong) UIView *rightView;

@property (nonatomic,strong) UIView *vLineView;

@property (strong, nonatomic) UIView *lineView;

@end

@implementation RegisterCodeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self!=nil){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.inputTextField];
        [self.contentView addSubview:self.lineView];
        [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
            make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
            make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
        }];
        
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.inputTextField);
            make.top.mas_equalTo(self.inputTextField.mas_bottom).mas_offset(5);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
        }];
       
    }
    return self;
}

- (void)setIsPassword:(BOOL)isPassword{
    [self.inputTextField setSecureTextEntry:isPassword];
}

- (void)setPlaceholder:(NSString *)placeholder{
    self.inputTextField.placeholder = placeholder;
    UIFont *font =  PingFangZhongChangGuiForFont(14);
    [self setPlaceholderStyleWithFont:font color:kGrayTextColor];
}

- (void)setPlaceholderStyleWithFont:(UIFont *)font color:(UIColor *)color {
    NSString *str = self.inputTextField.placeholder;
    if (IsEmptyStr(str)) {
        return;
    }
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentRight;
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font, NSParagraphStyleAttributeName:style}];
    self.inputTextField.attributedPlaceholder = attri;
}

- (void)setRightView:(UIView *)rightView size:(CGSize)size{
    [self setRightView:rightView size:size showVline:NO];
    /* if(_rightView){
     [_rightView removeFromSuperview];
     }
     _rightView = rightView;
     if(rightView){
     [self.contentView addSubview:_rightView];
     [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
     make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
     make.centerY.mas_equalTo(self.contentView);
     make.size.mas_offset(size);//CGSizeMake(90,36)
     }];
     [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
     make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
     make.centerY.mas_equalTo(self.contentView);
     make.right.mas_equalTo(_rightView.mas_left).mas_offset(-5);
     }];
     }else{
     [_rightView removeFromSuperview];
     [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
     make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
     make.centerY.mas_equalTo(self.contentView);
     make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
     }];
     }*/
}

- (void)setRightView:(UIView *)rightView size:(CGSize)size showVline:(BOOL)isShow{
    if(_rightView){
        [_rightView removeFromSuperview];
    }
    if(self.vLineView){
        [self.vLineView removeFromSuperview];
    }
    _rightView = rightView;
    if(rightView){
        [self.contentView addSubview:_rightView];
        if (isShow) {
            [self.contentView addSubview:self.vLineView];
            [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
                make.centerY.mas_equalTo(self.contentView);
                make.size.mas_offset(size);//CGSizeMake(90,36)
            }];
            [self.vLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(_rightView.mas_left).mas_offset(-10);
                make.top.mas_equalTo(self.contentView).mas_offset(12);
                make.bottom.mas_equalTo(self.contentView).mas_offset(-12);
                make.width.mas_offset(@0.5);
            }];
           
            [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.contentView).mas_offset(kRegisterMarginOffsetX);
                make.bottom.mas_equalTo(self.contentView).mas_offset(-12);
                make.right.mas_equalTo(self.vLineView.mas_left).mas_offset(-10);
            }];
            
            [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.inputTextField);
                make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-1);
                make.height.mas_equalTo(1);
                make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
            }];
        }else{
            [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.contentView).mas_offset(-16);
                make.centerY.mas_equalTo(self.contentView);
                make.size.mas_offset(size);//CGSizeMake(90,36)
            }];
            [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
                make.centerY.mas_equalTo(self.contentView);
                make.right.mas_equalTo(_rightView.mas_left).mas_offset(-5);
            }];
        }
    }else{
        [_rightView removeFromSuperview];
        [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
            make.centerY.mas_equalTo(self.contentView);
            make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
        }];
    }
}




- (void)textFieldDidChange:(UITextField *)textField{
    if (self.inputMaxLength>0) {
        if (textField.text.length > self.inputMaxLength) {
            textField.text = [textField.text substringToIndex:self.inputMaxLength];
        }
    }
    if (self.textChangeBlock) {
        self.textChangeBlock(self.tag,textField.text);
    }
}


- (UITextField *)inputTextField{
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc]init];
        [_inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_inputTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _inputTextField.font = PingFangZhongChangGuiForFont(15);
    }
    return _inputTextField;
}

MACRO_UIView_Lazyload(vLineView, [UIColor colorWithHexString:@"0xe9e9e9"])

MACRO_UIView_Lazyload(lineView, [UIColor colorWithHexString:@"0xe9e9e9"])

+ (CGFloat)heightForCell{
    return 53;
}

@end
